package com.ohuang.imghide.hidetank;

import android.graphics.Bitmap;
import android.graphics.Color;

import com.ohuang.imghide.util.BitmapUtil;

public class ColourHideTank {

    public static Bitmap create(Bitmap bitmap, Bitmap bitmap2) {
        if (bitmap != null && bitmap2 != null) {
            int with = Math.max(bitmap.getWidth(), bitmap2.getWidth());
            int height = Math.max(bitmap.getHeight(), bitmap2.getHeight());
            return getBitmap(bitmap, bitmap2, with, height);
        }
        return null;
    }
    public static Bitmap create(Bitmap bitmap, Bitmap bitmap2,float rw,float gw,float bw) {
        if (bitmap != null && bitmap2 != null) {
            int with = Math.max(bitmap.getWidth(), bitmap2.getWidth());
            int height = Math.max(bitmap.getHeight(), bitmap2.getHeight());
            return getBitmap(bitmap, bitmap2, with, height,rw,gw,bw);
        }
        return null;
    }

    public static Bitmap create(Bitmap bitmap, Bitmap bitmap2,int with,int height) {
        if (bitmap != null && bitmap2 != null) {
            return getBitmap(bitmap, bitmap2, with, height);
        }
        return null;
    }

    private static Bitmap getBitmap(Bitmap bitmap, Bitmap bitmap2, int with, int height) {
        return getBitmap(bitmap, bitmap2, with, height,0.333f,0.333f,0.333f);
    }

    private static Bitmap getBitmap(Bitmap bitmap, Bitmap bitmap2, int with, int height,float rw,float gw,float bw) {
        Bitmap scale = BitmapUtil.Scale(bitmap, with, height);
        Bitmap scale1 = BitmapUtil.Scale(bitmap2, with, height);
        int[] pix1 = new int[with * height];
        int[] pix2 = new int[with * height];
        int[] pix3 = new int[with * height];
        scale.getPixels(pix1, 0, with, 0, 0, with, height);
        scale1.getPixels(pix2, 0, with, 0, 0, with, height);
        Bitmap bitmap1 = Bitmap.createBitmap(with, height, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < pix1.length; i++) {
            pix3[i]= getColor(pix1[i],pix2[i],rw,gw,bw);
        }
        bitmap1.setPixels(pix3, 0, with, 0, 0, with, height);
        return bitmap1;
    }




    private static int getColor(int color, int color2,float rw,float gw,float bw) {
        int a= Color.alpha(color);
        int dl=(255-a);
        int r = Color.red(color)*a/255+dl;
        int g = Color.green(color)*a/255+dl;
        int b = Color.blue(color)*a/255+dl;

        int a1=Color.alpha(color2);
        int dl1=255-a1;
        int r1 = Color.red(color2)*a1/255+dl1;
        int g1 = Color.green(color2)*a1/255+dl1;
        int b1 = Color.blue(color2)*a1/255+dl1;

        int[] red = getGrayColor(r, r1);
        int[] green = getGrayColor(g, g1);
        int[] blue = getGrayColor(b, b1);
        int alpha = (int) (rw * red[0] + gw * green[0] + bw * blue[0]);
        return Color.argb(alpha, red[1], green[1], blue[1]);
    }

    /**
     * @param gray  0-255
     * @param gray2 0-255
     * @return data[0]为不透明度 data[1]单通道颜色值
     */
    private static int[] getGrayColor(int gray, int gray2) {
        float a = (255 + gray2 - gray) * 1.0f / 255;
        int t = (int) (gray2 * 1.0f / a);
        int[] data = new int[2];
        data[0] = (int) (a * 255 / 2);
        data[1] = t;
        return data;
    }

}
